package com.itheima.algorithm.binarytree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @author: TylerZhong
 * @description: 二叉树最大深度 - 使用层序遍历
 */
public class E05Leetcode104_3 {

    /**
     * 思路：
     *  1. 使用层序遍历，层数即最大深度
     * @param root
     * @return
     */
    public static int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        int max = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                if (poll.left != null) {
                    queue.offer(poll.left);
                }
                if (poll.right != null) {
                    queue.offer(poll.right);
                }
            }
            System.out.println();
            max++;
        }
        return max;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(
                new TreeNode(
                        new TreeNode(4),
                        2,
                        new TreeNode(5)
                ),
                1,
                new TreeNode(
                        new TreeNode(6),
                        3,
                        new TreeNode(7)
                )
        );
        int i = maxDepth(root);
        System.out.println(i);
    }
}
